<?php
/**
 * WpPackager.php
 * 
 * This file contains the WpPackager class providing functionality to define packages for projects
 * which can be included in your project files easily.
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage base
*/

if (function_exists("globalEmbed") !== true) {
	/**
	 * A wrapper function for require_once to be called from classes but provide global includings
	 * @param $embedPath String The path to embed
	 * @param $useRequire bool [optional] True to use require_once for including (default), otherwise include_once will
	 * be used
	 */
	function globalEmbed($embedPath, $useRequire = true)
	{
		if ($useRequire === true) {
			require_once($embedPath);
		} else {
			include_once($embedPath);
		}
 	}
}

/**
 * A class to provide package functionality for php files
 * 
 * This is the packager class which provides functionality to define packages for projects
 * which can be included in your project files easily.
 * 
 * How to use:
 * <code>
 * //At first a class is needed for the project to inherit the packagers methods
 * class MyProject extends WpPackager{
 * 		
 * 		//Define the project root
 * 		$ProjectRoot = dirname(__FILE__);
 * 
 * 		//Define packages and files here
 * 		protected $Components = array(
 * 			'*' => array('package1', 'package2', 'package3'),
 * 			'package1' => array('package11', 'package1/file1.php'),
 * 			'package11' => array('package1/package11/file1.php', 'package1/package11/file2.php'),
 * 			'package2' => array('package2/file1.php'),
 * 			'package3' => array('package3/file1.php')
 * 		);
 * }
 * 
 * //Now it is possible to include packages like
 * $o = new MyProject();
 * $o->using('package1'); //Includes package1 and also package11 as it is a subpackage of package1
 * $o->using('package2'); //Includes package2 which contains the file package2/file1.php
 * 
 * $o->using('*'); //Include all packages and files
 * 
 * </code>
 * 
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage base
 */
class WpPackager
{
		
	//####################################################################
	/**
	 * An array containing all possible packages and files in inclusion order
	 */
	protected $Components = array();
	
	//####################################################################
	/**
	 * An array containing all already included packages and files
	 */
	protected $IncludedComponents = array();
	
	/**
	 * @var The project path to be set by the extending class to get the packages root.
	 */
	protected $ProjectRoot = '';
	
	//####################################################################
	/**
	 * Recursively embeds packages and files using the global wpEmbed function
	 * @param $strPart String The package or file to embed 
	 */
	protected function embedComponent($strPart)
	{
		if (!array_key_exists($strPart, $this->Components)) {
			return false;
		}
		if (in_array($strPart, $this->IncludedComponents)) {
			return;
		}
		$this->IncludedComponents[] = $strPart;
		
		$arrTemp = $this->Components[$strPart];
		foreach ($arrTemp as $strEntry) {
			if (substr($strEntry, strlen($strEntry)-4) === '.php') {
				globalEmbed($this->ProjectRoot.'/'.$strEntry);
			} else {
				$this->embedComponent($strEntry);
			}
		}
	}
	
	//####################################################################
	/**
	 * Embeds the specified package or file - basically this is a wrapper for require_once
	 * @param $packageOrFile String The package or file to embed
	 */
	public function using($packageOrFile)
	{
		$this->embedComponent($packageOrFile);
	}
}

/* End of file WpPackager.php */
/* Location: ./wildpack/base/WpPackager.php */ 
